package com.share.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.share.admin.mapper.DictionaryMapper;
import com.share.admin.model.db.Dictionary;
import com.share.admin.model.vo.DictionaryPageReq;
import com.share.admin.service.IDictionaryService;
import com.share.common.common.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-09-22
 */
@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements IDictionaryService {
    private Map<String, Dictionary> codeMap = new HashMap<>();
    private Map<String, List<Dictionary>> gCodeMap = new HashMap<>();
    private Map<String, Dictionary> gCodeAndCodeMap = new HashMap<>();

    @Resource
    private DictionaryMapper dictionaryMapper;

    @Override
    public Object init() {
        LambdaQueryWrapper<Dictionary> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dictionary::getStatus, 1);
        queryWrapper.orderByAsc(Dictionary::getSort);//排序
        List<Dictionary> list = dictionaryMapper.selectList(queryWrapper);
        Map<String, Dictionary> codeMap_ = new HashMap<>();
        Map<String, Dictionary> gCodeAndCodeMap_ = new HashMap<>();
        Map<String, List<Dictionary>> gCodeMap_ = new HashMap<>();
        for (Dictionary record : list) {
            codeMap_.put(record.getCode(), record);
            gCodeAndCodeMap_.put(record.getGCode() + "-" + record.getCode(), record);

            //分组的处理
            List<Dictionary> tmpList = gCodeMap_.get(record.getGCode());
            if (tmpList == null) {
                tmpList = new LinkedList<>();
                gCodeMap_.put(record.getGCode(), tmpList);
            }
            tmpList.add(record);
        }

        //赋值
        this.codeMap = codeMap_;
        this.gCodeAndCodeMap = gCodeAndCodeMap_;
        this.gCodeMap = gCodeMap_;

        return list;
    }

    @Override
    public Dictionary getByGCodeAndCode(String gCode, String code) {
        return gCodeAndCodeMap.get(gCode + "-" + code);
    }

    @Override
    public Dictionary getByCode(String code) {
        return codeMap.get(code);
    }

    @Override
    public List<Dictionary> listByGCode(String gCode) {
        return gCodeMap.get(gCode);
    }

    @Override
    public Map<String, List<Dictionary>> getAllGCode() {
        return gCodeMap;
    }

    @Override
    public Page pageList(DictionaryPageReq req) {
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        LambdaQueryWrapper<Dictionary> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(req.getName())) {
            queryWrapper.like(Dictionary::getName, req.getName());
        }
        if (StringUtils.isNotBlank(req.getCode())) {
            queryWrapper.like(Dictionary::getCode, req.getCode());
        }
        if (StringUtils.isNotBlank(req.getGCode())) {
            queryWrapper.like(Dictionary::getGCode, req.getGCode());
        }
        if (StringUtils.isNotBlank(req.getName())) {
            queryWrapper.like(Dictionary::getName, req.getName());
        }
        if (StringUtils.isNotBlank(req.getValue())) {
            queryWrapper.like(Dictionary::getValue, req.getValue());
        }
        if (StringUtils.isNotBlank(req.getStatus())) {
            queryWrapper.like(Dictionary::getStatus, req.getStatus());
        }
        List<Dictionary> list = dictionaryMapper.selectList(queryWrapper);
        return new Page<>(list, new PageInfo<>(list).getTotal(), req);
    }
}
